// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 66 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function131
reserve function Eggman_Function132
reserve function Eggman_Function133

// Static Values

// Tables

function Eggman_Function131
	object.rotation += 2
	object.rotation &= 511
	temp0 = object.rotation
	temp0 <<= 1
	temp0 += 128
	Sin(object.ypos, temp0)
	object.ypos <<= 9
	object.ypos += object.value4
end function


function Eggman_Function132
	object.rotation += 4
	object.rotation &= 511
	Sin(object.ypos, object.rotation)
	object.ypos <<= 9
	object.ypos += object.value3
end function


function Eggman_Function133
	object[-6].xpos = object.xpos
	object[-6].ypos = object.ypos
	object[-6].ypos += object.value7
	Sin(temp1, object.rotation)
	temp1 >>= 2
	temp1 += 128
	temp0 = 16
	Cos(object[-5].xpos, temp1)
	object[-5].xpos *= temp0
	object[-5].xpos <<= 7
	object[-5].xpos += object[-6].xpos
	Sin(object[-5].ypos, temp1)
	object[-5].ypos *= temp0
	object[-5].ypos <<= 7
	object[-5].ypos += object[-6].ypos
	temp0 += 16
	Cos(object[-4].xpos, temp1)
	object[-4].xpos *= temp0
	object[-4].xpos <<= 7
	object[-4].xpos += object[-6].xpos
	Sin(object[-4].ypos, temp1)
	object[-4].ypos *= temp0
	object[-4].ypos <<= 7
	object[-4].ypos += object[-6].ypos
	temp0 += 16
	Cos(object[-3].xpos, temp1)
	object[-3].xpos *= temp0
	object[-3].xpos <<= 7
	object[-3].xpos += object[-6].xpos
	Sin(object[-3].ypos, temp1)
	object[-3].ypos *= temp0
	object[-3].ypos <<= 7
	object[-3].ypos += object[-6].ypos
	temp0 += 16
	Cos(object[-2].xpos, temp1)
	object[-2].xpos *= temp0
	object[-2].xpos <<= 7
	object[-2].xpos += object[-6].xpos
	Sin(object[-2].ypos, temp1)
	object[-2].ypos *= temp0
	object[-2].ypos <<= 7
	object[-2].ypos += object[-6].ypos
	temp0 += 32
	Cos(object[-1].xpos, temp1)
	object[-1].xpos *= temp0
	object[-1].xpos <<= 7
	object[-1].xpos += object[-6].xpos
	Sin(object[-1].ypos, temp1)
	object[-1].ypos *= temp0
	object[-1].ypos <<= 7
	object[-1].ypos += object[-6].ypos
	object[-1].rotation = temp1
	if object.rotation >= 256
		object.direction = FLIP_NONE
	else
		object.direction = FLIP_X
	end if
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object[-1].ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
		temp0 = object[-1].ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		temp0 = object.iypos
		temp0 += 64
		if screen.yoffset > temp0
			if object[0].xpos > object[-1].xpos
				object.animation = 1
				object.value1 = 8
				ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
				object[26].priority = PRIORITY_ACTIVE
				object.state++
			end if
		end if
		break
	case 1
		object.ypos += 0x10000
		if object.ypos >= object.value3
			object.ypos = object.value3
			object.value0 = 96
			object[-1].value6 = 1
			object.state++
		end if
		break
	case 2
		if object.value0 > 0
			object.value0--
			object.xpos = object.value0
			object.xpos <<= 16
			object.xpos += object[-1].xpos
			temp0 = object.value0
			temp0 <<= 7
			temp0 /= 96
			Cos(object.ypos, temp0)
			object.ypos *= -0xB00
			object.ypos += object.value3
		else
			object.xpos = object[-1].xpos
			object.value2 = object[-1].xpos
			object.ypos = object.value4
			object.value4 -= 0x40000
			object.state++
			object[-1].type = TypeName[Wrecking Ball]
			object[-1].ypos = object.ypos
			object[-6].type = TypeName[Boss Lights]
			object[-6].ypos = object.ypos
			object.animation = 2
			object[-1].value6 = 0
		end if
		break
	case 3
		if object.value6 < 0x7C0000
			CallFunction(Eggman_Function131)
			object.value6 += 0x10000
			if object.value7 < 0x1C0000
				object.value7 += 0x8000
			end if
			if object.value6 > 0x2C0000
				object[-5].type = TypeName[Boss Chain]
			end if
			if object.value6 > 0x3C0000
				object[-4].type = TypeName[Boss Chain]
			end if
			if object.value6 > 0x4C0000
				object[-3].type = TypeName[Boss Chain]
			end if
			if object.value6 > 0x5C0000
				object[-2].type = TypeName[Boss Chain]
			end if
			object[-1].ypos = object.ypos
			object[-1].ypos += object.value6
			object[-6].ypos = object.ypos
			object[-6].ypos += object.value7
			object[-5].ypos = object[-6].ypos
			object[-5].ypos += 0x100000
			object[-4].ypos = object[-5].ypos
			object[-4].ypos += 0x100000
			object[-3].ypos = object[-4].ypos
			object[-3].ypos += 0x100000
			object[-2].ypos = object[-3].ypos
			object[-2].ypos += 0x100000
			object.animationTimer %= 10
		else
			object.rotation = 256
			CallFunction(Eggman_Function131)
			CallFunction(Eggman_Function133)
			object[-1].value1 = object.value5
			object[-1].value1 >>= 2
			object[-1].value2 = object.value5
			object[-1].value3 = object.value5
			object[-1].value3 *= 64
			temp0 = object[-1].value3
			temp0 >>= 1
			object.value2 -= temp0
			object.state++
			object.animation = 1
			object[-1].value6 = 1
			object[-1].state = 1
		end if
		break
	case 4
		CallFunction(Eggman_Function131)
		CallFunction(Eggman_Function133)
		if object.xpos > object.value2
			object.xpos -= object[-1].value1
			if object.xpos < object.value2
				object.xpos = object.value2
			end if
		else
			object[-1].value6 = 0
			object.value2 += object[-1].value3
			object.state++
		end if
		break
	case 5
		CallFunction(Eggman_Function131)
		CallFunction(Eggman_Function133)
		if object.xpos < object.value2
			if object.rotation >= 128
				object[-1].value6 = 1
				object.xpos += object[-1].value2
				if object.xpos > object.value2
					object.xpos = object.value2
				end if
			end if
		else
			object[-1].value6 = 0
			object.value2 -= object[-1].value3
			object.state++
		end if
		break
	case 6
		CallFunction(Eggman_Function131)
		CallFunction(Eggman_Function133)
		if object.xpos > object.value2
			if object.rotation >= 384
				object[-1].value6 = 1
				object.xpos -= object[-1].value2
				if object.xpos < object.value2
					object.xpos = object.value2
				end if
			end if
		else
			object[-1].value6 = 0
			object.value2 += object[-1].value3
			object.state--
		end if
		break
	case 7
		object.value0++
		temp0 = object.value0
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Explosion], 0)
		end if
		if object.value0 == 180
			object.animation = 5
			object.animationTimer = 0
			object.value0 = 0
			object.direction = FLIP_X
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.state++
		end if
		break
	case 8
		object.ypos += object[-1].value5
		object[-1].value5 += 0x1800
		object.value0++
		if object.value0 == 38
			object[-1].value5 = 0
			object.value0 = 0
			object.animationTimer = 0
			object.state++
		end if
		break
	case 9
		if object.value0 < 48
			object.ypos += object[-1].value5
			object[-1].value5 -= 0x800
			object.value0++
		else
			object.value0 = 0
			object[-1].value5 = 0
			object.rotation = 0
			object.value3 = object.ypos
			object.state++
		end if
		break
	case 10
		CallFunction(Eggman_Function132)
		if object.value0 < 8
			object.value0++
		else
			object.value0 = 0
			object.animation = 6
			object.animationTimer = 0
			object[-1].value6 = 2
			object[-1].value7 = 0
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 11
		CallFunction(Eggman_Function132)
		object.xpos += 0x40000
		object.value3 -= 0x4000
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		break
	end switch
	if object[-1].value4 > 0
		object[-1].value4--
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object[-1].value4 == 0
			CheckEqual(object[currentPlayer].animation, ANI_HURT)
			temp0 = checkResult
			CheckEqual(object[currentPlayer].animation, ANI_DYING)
			temp0 |= checkResult
			CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
			temp0 |= checkResult
			if temp0 != 0
				if object.animation != 2
					object.animation = 2
					object.animationTimer = 0
				end if
			end if
			switch object.state
			case 1
			case 2
			case 3
			case 4
			case 5
			case 6
				BoxCollisionTest(C_TOUCH, object.entityPos, -20, -16, 20, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value1--
						if object.value1 == 0
							player.score += 1000
							object.animationTimer = 0
							object.animation = 4
							temp0 = object.entityPos
							temp0 -= 2
							ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-2].xpos, object[-2].ypos)
							temp0--
							ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-3].xpos, object[-3].ypos)
							temp0--
							ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-4].xpos, object[-4].ypos)
							temp0--
							ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-5].xpos, object[-5].ypos)
							temp0--
							ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-6].xpos, object[-6].ypos)
							object[-1].state = 2
							object[-1].value6 = 0
							object.value0 = 0
							object.state = 7
						else
							object.animationTimer = 0
							object.animation = 3
							object[-1].value4 = 32
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
			end switch
			if object.state < 11
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				temp0 += object[currentPlayer].xpos
				temp1 = stage.curXBoundary2
				temp1 <<= 16
				if temp0 > temp1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].xpos = temp1
					temp0 = object[currentPlayer].collisionRight
					temp0 <<= 16
					object[currentPlayer].xpos -= temp0
				end if
			end if
		end if
	next
end event


event ObjectDraw
	switch object.animation
	case 1
		temp0 = object.animationTimer
		if saveRAM[495] == 0
			temp0 /= 6
		else
			temp0 /= 24
		endif
		object.animationTimer++
		if saveRAM[495] == 0
			object.animationTimer %= 12
		else
			object.animationTimer %= 48
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		if saveRAM[495] == 0
			temp0 %= 10
			temp0 /= 5
		else
			temp0 %= 20
			temp0 /= 10
		endif
		temp0 += 9
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 7
		object.animationTimer++
		object.animationTimer %= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	switch object[-1].value6
	case 1
		temp0 = object[-1].value7
		temp0 >>= 2
		temp0 += 11
		object[-1].value7++
		object[-1].value7 &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object[-1].value7
		temp0 /= 3
		temp0 += 11
		object[-1].value7++
		if object[-1].value7 > 21
			object[-1].value7 = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	if saveRAM[495] == 0
		LoadSpriteSheet("Global/Eggman.gif")
	else
		LoadSpriteSheet("Global/EggRobo.gif")
	endif
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 131, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 58)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 115)
	SpriteFrame(-28, -32, 64, 56, 66, 115)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].type == TypeName[Eggman]
			object[arrayPos0].priority = PRIORITY_XBOUNDS
			temp0 = screen.xcenter
			temp0 -= 160
			temp0 -= 8
			if temp0 < 0
				temp0 = 0
			end if
			if temp0 > 24
				temp0 = 24
			end if
			object[arrayPos0].value5 = temp0
			object[arrayPos0].value5 += 32
			object[arrayPos0].value5 *= 0x800
			temp0 <<= 16
			arrayPos1 = arrayPos0
			arrayPos1++
			object[arrayPos1].xpos -= temp0
			arrayPos1++
			object[arrayPos1].xpos += temp0
			object[arrayPos0].value3 = object[arrayPos1].ypos
			object[arrayPos0].value3 -= 0x440000
			object[arrayPos0].value4 = object[arrayPos1].ypos
			object[arrayPos0].value4 -= 0x5A0000
			arrayPos1 = arrayPos0
			arrayPos1--
			object[arrayPos0].value2 = object[arrayPos1].xpos
			object[arrayPos0].value2 += 0x600000
			object[arrayPos0].xpos = object[arrayPos0].value2
			object[arrayPos0].ypos = object[arrayPos0].value4
			object[arrayPos0].ypos -= 0xC00000
		end if
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
